<template>
  <div class="main_page flex_ac_jc">
    <img src="../../static/logo.png" alt="" class="logo">
    <div class="login_text flex1">
      <div class="tt">Hi，你好！</div>
      <div class="pp">欢迎登录恒诺巡访客户端</div>
    </div>
    <div class="login_box">
      <div class="tt color">找回密码</div>

      <el-form :model="ruleForm" :rules="rules"  ref="ruleForm" class="login_form pad_t40">
        <el-form-item label="" prop="mobile">
          <div class="flex_ac">
            <div class="flex1">
              <el-input v-model="ruleForm.mobile" zi placeholder="请输入手机号"></el-input>
            </div>
          </div>
        </el-form-item>
        <el-form-item label="" prop="code">
          <div class="flex_ac">
            <div class="flex1"><el-input type="text" v-model="ruleForm.code" placeholder="请输入验证码"></el-input></div>
            <a href="javascript:;" @click='get_code' class='get_code2' :class="is_wait?'disabled':''">{{is_wait? time+"s再次获取":"获取验证码"}} </a>
          </div>

        </el-form-item>
        <el-form-item label="" prop="password">
          <el-input type="password" v-model="ruleForm.password" show-password placeholder="请输入新密码"></el-input>
        </el-form-item>
        <div class="flex_ac_jc forget">
          <a href="javascript:;" @click="$router.go(-1);" class="margin_a">返回登录</a>
        </div>
        <div class="pad_t30"></div>
        <div class="pad_t40">
          <el-button type="primary" :loading="is_ajax" style="width: 100%; height: 60px;" @click="submitForm('ruleForm')">确认</el-button>
        </div>

      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: 'login',
  data () {

    return {
      is_ajax:false,
      timeset:null,
      is_ajax: false,
      is_wait: false,
      time: 60,
      ruleForm: {
        mobile: '',
        password:'',
        code:''
      },
      rules: {
        code: [
          { required: true, message: '请输入验证码', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '请输入账号', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入新密码', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    let  state=this.getState();
    if(state.token){
      this.$router.replace({path:'/'})
    }
  },
  methods:{
    get_code: function() {
    	const _this = this;
    	if (_this.is_wait) {
    		return false
    	}
    	const _mobile = _this.ruleForm.mobile;
    	if (_mobile == "") {
        _this.tip_show('请输入手机号','error')
    		return false
    	}
    	if (!(/^1[3456789]\d{9}$/.test(_mobile))) {
        _this.tip_show('手机号格式错误','error')
    		return false
    	}
    	_this.is_wait = true;
    	_this.end_time();
    	_this.axios.post("/api/Code/getPcLoginBySMS", {
    		mobile: _this.ruleForm.mobile,
        type:2
    	}).then(res => {
    		if (res.status != "200") {
    			_this.time = 60;
    			clearInterval(_this.timeset);
    			_this.is_wait=false;
          _this.tip_show(res.message || res.msg,'error')
    		}else{
          _this.tip_show(res.message || res.msg,'success')
        }
    	}).catch(res => {
    		// 失败进行的操作
    		console.log(res)
    	})
    },
    end_time: function() {
    	const _this = this;
    	_this.timeset = setInterval(function() {
    		if (_this.time == 1) {
    			_this.time=60;
    			clearInterval(_this.timeset);
    			_this.is_wait=false
    		}
    		_this.time-=1
    	}, 1000)
    },
    submitForm(formName) {
      const _this=this;
      this.$refs[formName].validate((valid) => {
        if (valid) {
          _this.is_ajax=true
          _this.axios.post("/api/Forget/PcforSMS", {
            mobile: _this.ruleForm.mobile,
            code: _this.ruleForm.code,
            password: _this.ruleForm.password,
            type:2
          }).then(function(data) {
            if(data){
              _this.tip_show(data.message,'success');
              _this.$router.replace({path:'/login'})
            }
            _this.is_ajax=false;
          }).catch(function(error) {
            _this.is_ajax=false
            console.log(error);
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.main_page{position: fixed; left: 0; top: 0;width: 100%; height: 100%; padding: 0 230px; color: #fff; background: url(../../static/login_bg.jpg) center center; background-size: cover;}
.logo{height: 66px; position: absolute; left: 48px; top: 48px;}
.login_text .tt{font-size: 60px; margin-bottom: 20px;}
.login_text .pp{font-size: 40px;}
.login_box{border-radius: 20px; background: #fff; padding: 60px; height: 600px; width: 500px;}
.login_box .tt{font-size: 30px;  font-weight: bold;}
@media (max-width: 1700px){
  .main_page{padding: 0 170px;}
.login_text .tt{font-size: 40px;}
.login_text .pp{font-size: 20px;}
}
@media (max-width: 1500px){
.login_box{width: 400px;}
}
.forget a{color: #828696; text-decoration: none;}
.forget a:hover{color: #004EA2;}

.get_code2{color: #004EA2;;}
</style>
